<template>
  <div>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params['customer_address_code.codelike']"
          clearable
          auto-top
          placeholder="所属地"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.customer_name"
          :clearable="true"
          placeholder="主体名称查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.real_name"
          :clearable="true"
          placeholder="姓名"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button type="primary" icon="el-icon-upload2" @click="handleUpload()">导入</el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleDownload()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    >
      <template slot="view_detail" slot-scope="scope">
        <button
          type="button"
          class="btn btn-text text-primary py-0 px-2"
          @click="handleViewDetail(scope.row)"
        >
          详情列表
        </button>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <import-excel
      :visible.sync="dialogImportVisible"
      title="家庭成员导入"
      :post-api="importUrl"
      excel-template="/static/files/家庭成员导入模板.xlsx"
      @success="getData(params.index)"
    ></import-excel>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryTrouble } from '@vimi/utils-api'
import SelectAddress from '@/components/select/SelectAddress'
import ImportExcel from '@/components/ImportExcel'
import { enumSexType } from '@/credit/plugins/enum'
export default {
  components: {
    SelectAddress,
    ImportExcel,
  },
  props: {},
  data() {
    return {
      tableName: 'datum_customer_family_emp_warehouse',
      tableId: '',
      params: {
        index: 1,
        size: 20,
        customer_related_name: '',
        submit_person_name: '',
        customer_address_code: '',
      },
      loading: false,
      tableData: [],
      tableCount: 0,
      tableColumns: [
        { attrs: { label: '地址', prop: 'customer_address_detail', minWidth: '160px' } },
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '160px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '姓名', prop: 'real_name' } },
        { attrs: { label: '性别', prop: 'sex' }, process: v => enumSexType[v] },
        { attrs: { label: '年龄', prop: 'age' } },
        { attrs: { label: '关系', prop: 'relation' } },
        { attrs: { label: '职业', prop: 'career' } },
        { attrs: { label: '联系电话', prop: 'phone_number' } },
        { attrs: { label: '备注', prop: 'remark' } },
        // {
        //   attrs: { label: '操作', align: 'center', minWidth: '60px' },
        //   operations: [
        //     { content: '删除', icon: 'fa fa-trash', attrs: { 'class': 'text-danger py-0 px-2' }, event: this.handleDelete }
        //   ]
        // }
      ],

      // 编辑
      queryId: '',

      // 导入
      dialogImportVisible: false,
      importUrl: '',
      // 导出
      exportLoading: false,
    }
  },
  computed: {
    ...mapGetters(['org']),
  },
  watch: {},
  created() {
    this.params['customer_address_code.codelike'] = this.$utils.getAddressCode()
    this.importUrl =
      '/api/credit/import/' +
      queryTrouble(this.tableName, {
        importKey: `${this.tableName}_template`,
      })
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params)
      )
      this.loading = false
      const data = (res && res.data) || {}
      this.tableData = data.data || []
      this.tableCount = data.totalCount || 0
    },
    handleBeforeClose(done) {
      this.queryId = ''
      done()
    },
    handleUpload() {
      this.dialogImportVisible = true
    },
    async handleDownload() {
      const { index, size, ...params } = this.params // eslint-disable-line
      const exportParams = {
        service: this.$serviceName,
        table: this.tableName,
        params: params,
        filename: '家庭成员.xlsx',
      }

      this.exportLoading = true
      await this.$utils.promise(this.$api.exportData(exportParams))
      this.exportLoading = false
    },
  },
}
</script>

<style lang="scss" scoped></style>
